from django.contrib.auth import authenticate, login, logout as out
from django.contrib.auth.models import User
from django.db import IntegrityError
from django.shortcuts import render, redirect
from django.views import View

from user.forms import UserLRForm


class UserView(View):
    def get(self, request):
        print(self)
        form = UserLRForm()
        return render(request, 'user/register.html', context={'form': form})

    def post(self, request):

        form = UserLRForm(request.POST)
        # username = request.POST.get('username')
        # password = request.POST.get('password')
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            # if not username:
            #     message = '注册失败，请输入用户名'
            # elif 2 > len(username) or len(username) > 10:
            #     message = '注册失败，用户名长度为2-10'
            # elif 6 > len(password) or len(password) > 20:
            #     message = '注册失败，密码长度为6-20'
            # else:
            try:
                user = User(username=username)
                user.set_password(password)
                user.save()
                message = '注册成功'
            except IntegrityError:
                message = '用户名已存在，请重新输入'

        return render(request, 'user/register.html', context={"message": message, 'form': form})


class LoginView(View):
    def get(self, request):
        form = UserLRForm()
        # print(request)
        return render(request, 'user/login.html', context={'form': form})

    def post(self, request):
        # username = request.POST.get('username')
        # password = request.POST.get('password')
        # if not username:
        #     message = '登录失败，请输入用户名'
        # elif 2 > len(username) or len(username) > 10:
        #     message = '登录失败，用户名长度为2-10'
        # elif 6 > len(password) or len(password) > 20:
        #     message = '注册失败，密码长度为6-20'
        # else:

        form = UserLRForm(request.POST)

        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user:
                login(request, user)
                next_url = request.GET.get('next')
                # print(request)
                # print(next_url)
                if next_url:
                    return redirect(next_url)
                else:
                    return redirect('index')
            # try:
            #     user = User.objects.get(username=username)
            # except User.DoesNotExist:
            #     message = '账号不存在'
            # else:
            #     if user.check_password(password):
            #         request.session['username'] = username
            #         # request.session.set_expiry(300)
            #         return redirect('index')
            #     else:
            #         message = '密码错误'

        return render(request, 'user/login.html', context={ 'form': form})


def logout(request, **kwargs):
    # request.session.flush()
    out(request)
    return redirect('login')
